﻿@{
    var pageindex = Convert.ToInt32(ViewBag.PageIndex);
    var pagecount = Convert.ToInt32(ViewBag.PageCount);
    pagecount = pagecount == 0 ? 1 : pagecount;
    pageindex = pageindex > pagecount ? pagecount : pageindex;
    var path = Context.Request.Path.Value;
    var query = string.Empty;
    var querys = Context.Request.Query;
    foreach (var item in querys)
    {
        if (!item.Key.Equals("page"))
        {
            query += $"{item.Key}={item.Value}&";
        }
    }
    query = query == string.Empty ? "?" : "?" + query;
    path += query;
    var pagestart = pageindex - 2 > 0 ? pageindex - 2 : 1;
    var pageend = pagestart + 5 >= pagecount ? pagecount : pagestart + 5;
}
<ul class="pagination">
    <li class="prev previous_page @(pageindex == 1 ? "disabled" : "")">
        <a href="@(pageindex==1?"#":$"{path}page={pageindex - 1}")">&#8592; 上一页</a>
    </li>
    <li @(pageindex == 1 ? "class=active" : "")><a rel="start" href="@(path)page=1">1</a></li>
    @if (pagestart > 2)
    {
        <li class="disabled"><a href="#">&hellip;</a></li>
    }
    @for (int i = pagestart; i < pageend; i++)
    {
        if (i > 1)
        {
            <li @(pageindex == i ? "class=active" : "")><a rel="next" href="@(path)page=@i">@i</a></li>
        }
    }
    @if (pageend < pagecount)
    {
        <li class="disabled"><a href="#">&hellip;</a></li>
    }
    @if (pagecount > 1)
    {
        <li @(pageindex == pagecount ? "class=active" : "")><a rel="end" href="@(path)page=@pagecount">@pagecount</a></li>
    }
    <li class="next next_page @(pageindex==pagecount?"disabled":"")">
        <a rel="next" href="@(pageindex==pagecount?"#":$"{path}page={pageindex + 1}")">下一页 &#8594;</a>
    </li>
</ul>